"""
    xfree-script
    ~~~~~~~~~~~~~~~~~~~~~
    文件描述：XFree启动脚本源文件
    作者：xfree
    邮箱：cy_83k@qq.com
"""
import re
import sys

try:
    from importlib.metadata import distribution
except ImportError:
    try:
        from importlib_metadata import distribution
    except ImportError:
        from pkg_resources import load_entry_point

__requires__ = 'xfree'


def importlib_load_entry_point(spec, group, name):
    """导入库加载入口点
    ：param spec: 库的规范名称
    ：param group: 组名称
    ：param name: 入口点名称
    """
    dist_name, _, _ = spec.partition('==')

    matches = (
        entry_point
        for entry_point in distribution(dist_name).entry_points
        if entry_point.group == group and entry_point.name == name
    )
    return next(matches).load()


# 查找全局是否有load_entry_point，如果没有添加load_entry_point的值为importlib_load_entry_point
globals().setdefault('load_entry_point', importlib_load_entry_point)

if __name__ == '__main__':
    # 去除启动脚本的扩展名或script后缀
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(load_entry_point('XFree', 'console_scripts', 'xfree')())
